package com.changgou.service.goods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.goods.pojo.TbBrand;
import com.changgou.service.goods.dao.BrandMapper;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, TbBrand> implements BrandService {
    @Resource
    private BrandMapper brandMapper;
    /**
     * 品牌列表查询
     * @return
     */
    @Override
    public List<TbBrand> findList() {

        List<TbBrand> tbBrandList = this.list();
        return tbBrandList;
    }

    /**
     * 根据id查询品牌信息
     * @param id
     * @return
     */
    @Override
    public TbBrand findById(Integer id) {
        TbBrand tbBrand = this.getById(id);
        return tbBrand;
    }

    /**
     * 品牌新增
     * @param tbBrand
     */
    @Override
    @Transactional
    public void add(TbBrand tbBrand) {
        save(tbBrand);
    }

    /**
     * 品牌修改
     * @param tbBrand
     */
    @Override
    @Transactional
    public void update(TbBrand tbBrand) {
        updateById(tbBrand);
    }

    /**
     * 根据id删除品牌信息
     * @param id
     */
    @Override
    @Transactional
    public void delById(Integer id) {
        removeById(id);
    }

    /**
     * 品牌列表条件查询
     * @param searchMap
     * @return
     */
    @Override
    public List<TbBrand> list(Map<String, Object> searchMap) {

        List<TbBrand> tbBrands = lambdaQuery()
                .like(searchMap.get("name") != null, TbBrand::getName, searchMap.get("name"))
                .eq(searchMap.get("letter") != null, TbBrand::getLetter, searchMap.get("letter"))
                .list();
//        Example example = new Example(Brand.class);
//        //封装查询条件
//        Example.Criteria criteria = example.createCriteria();
//        if (searchMap!=null){
//            //品牌名称(模糊) like  %
//            if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
//                criteria.andLike("name","%"+searchMap.get("name")+"%");
//            }
//            //按照品牌首字母进行查询(精确)
//            if (searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
//                criteria.andEqualTo("letter",searchMap.get("letter"));
//            }
//        }
//        List<Brand> brandList = brandMapper.selectByExample(example);
        return tbBrands;
    }

    @Override
    public Page<TbBrand> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<TbBrand> page1 = (Page<TbBrand>) this.list();
        return page1;
    }

    @Override
    public Page<TbBrand> findPage(Map<String, Object> searchMap, int page, int size) {
        //设置分页
        PageHelper.startPage(page,size);

        //条件查询
        List<TbBrand> tbBrands = lambdaQuery()
                .like(searchMap.get("name") != null, TbBrand::getName, searchMap.get("name"))
                .eq(searchMap.get("letter") != null, TbBrand::getLetter, searchMap.get("letter"))
                .list();
        Page<TbBrand> pageInfo = (Page<TbBrand>) tbBrands;

//        Example example = new Example(Brand.class);
//        Example.Criteria criteria = example.createCriteria();
//        if (searchMap!=null){
//            //设置品牌名称模糊查询
//            if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
//                criteria.andLike("name","%"+searchMap.get("name")+"%");
//            }
//            //设置品牌首字母的精确查询
//            if (searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
//                criteria.andEqualTo("letter",searchMap.get("letter"));
//            }
//        }
//        Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);
        return pageInfo;
    }


    @Override
    public List<Map> findListByCategoryName(String categoryName) {
        return brandMapper.findListByCategoryName(categoryName);
    }
}
